package com.kkd.kkdapp.activity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.MyTotalAssetsAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.AccountData1;
import com.kkd.kkdapp.http.entity.AllAccountInfo;

import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpClient;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.utils.UIEmptyHandleUtil;
import com.kkd.kkdapp.view.KkdaiRoundProgressBar;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * 
 * @data 2014-03-09
 * 
 * @author mty
 * 
 * @Name 我的总账户
 * 
 */
public class MyTotalAssetsActivity extends BaseActivity implements
		OnClickListener {
	private final static int UPDATE_UI = 1;
	private final static int CODE_ELSE = 2;

	private RelativeLayout return_img; // 返回键
	private KkdaiRoundProgressBar progressbar; // 进度条
	private List<String> tvArr = new ArrayList<String>();
	private List<String> colorArr = new ArrayList<String>();
	private List<String> valueArr = new ArrayList<String>();
	private AccountData1 mAccountData1;
	private TextView regular_estimate2;
	private TextView toal_tv;
	private TextView myAssets_tv;
	private ListView listview;
	private DecimalFormat df = new DecimalFormat("0.00");
	private RelativeLayout emptyView;
	private ScrollView nonUullView;
	private ImageView id_no_content;
	private TextView id_no_content_text;

	private Handler mhandle = new Handler() {
		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.Handler#handleMessage(android.os.Message)
		 */
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UPDATE_UI:
				UIEmptyHandleUtil.uiNullDispose(false, nonUullView, emptyView,
						id_no_content, id_no_content_text, "", 0);
				displayCacheData();
				break;
			case CODE_ELSE:
				int statusCode = (Integer) msg.obj;
				if (statusCode == KkdHttpClient.NO_NET) {
					UIEmptyHandleUtil.uiNullDispose(true, nonUullView,
							emptyView, id_no_content, id_no_content_text,
							"网络错误，请稍后重试", R.drawable.img_null_net);
				} else if (statusCode == KkdHttpClient.RESOLUTION_EXCEPTION) {
					UIEmptyHandleUtil.uiNullDispose(true, nonUullView,
							emptyView, id_no_content, id_no_content_text,
							"服务异常，请稍后重试", R.drawable.img_null_net);
				}
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_total_assets);
		initView();
		displayCacheData();
	}

	private void displayCacheData() {
		String json = PreferenceUtils.getInstance().getMyproperty(this);
		if (json == null || "".equals(json)) {
			requestMyAssets();
		} else {
			Gson gson = new Gson();
			AllAccountInfo mAllAccountInfo = gson.fromJson(json,
					AllAccountInfo.class);
			mAccountData1 = mAllAccountInfo.getAppData().getAccountData1();
			for (int i = 1; i < mAccountData1.getValueStrArr().size(); i++) {
				valueArr.add(mAccountData1.getValueStrArr().get(i));
			}
			for (int i = 1; i < mAccountData1.getNameArr().size(); i++) {
				tvArr.add(mAccountData1.getNameArr().get(i));
			}
			colorArr.addAll(mAccountData1.getColorArr());
			progressbar.setPropressArray(valueArr);
			progressbar.setcolorArray(mAccountData1.getColorArr());
			// 总资产
			toal_tv.setText(mAccountData1.getNameArr().get(0) + "(元)");
			myAssets_tv.setText(mAccountData1.getValueStrArr().get(0));
			MyTotalAssetsAdapter adapter = new MyTotalAssetsAdapter(this,
					tvArr, colorArr, valueArr);
			listview.setAdapter(adapter);
		}
	}

	/****************
	 * 初始化控件
	 ****************/
	public void initView() {
		return_img = (RelativeLayout) this.findViewById(R.id.return_img);
		progressbar = (KkdaiRoundProgressBar) this
				.findViewById(R.id.progressbar);
		progressbar.setRoundWidth(SystemUtils.dip2px(this, 25));
		toal_tv = (TextView) this.findViewById(R.id.toal_tv);
		myAssets_tv = (TextView) this.findViewById(R.id.myAssets_tv);
		emptyView = (RelativeLayout) this.findViewById(R.id.emptyView);
		nonUullView = (ScrollView) this.findViewById(R.id.nonUullView);
		id_no_content = (ImageView) this.findViewById(R.id.id_no_content);
		id_no_content_text = (TextView) this
				.findViewById(R.id.id_no_content_text);
		listview = (ListView) this.findViewById(R.id.listview);
		listview.setDividerHeight(0);
		listview.setFocusable(false);
		setListener();
	}

	/***************
	 * 注册监听事件
	 ***************/
	public void setListener() {
		return_img.setOnClickListener(this); // 返回键
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.return_img: // 返回键
			finish();
			break;
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		progressbar.initView(0);
		progressbar.invalidate();
	}

	/*
	 * 请求我的总收益
	 */
	private void requestMyAssets() {
		String customerIdAndToken = PreferenceUtils.getInstance()
				.getcustomerId(this);
		String[] cat = customerIdAndToken.split("&");
		if (cat == null || cat.length < 2)
			return;
		KkdHttpMagager.getInstance().getManageManager(new HttpCallBack() {

			@Override
			public void onSuccess(int statusCode, Object res) {
				mhandle.sendEmptyMessage(UPDATE_UI);

			}

			@Override
			public void onFailure(int statusCode, String responseBody) {
				Message msg = new Message();
				msg.obj = statusCode;
				msg.what = CODE_ELSE;
				mhandle.sendMessage(msg);
				// AppUtil.makeToastForCustomer(MyTotalAssetsActivity.this,
				// R.string.abnormal);
			}
		});
	}
}
